package chapter014;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * ArrayList
 *
 * 参考链接:
 * [Java 集合框架] https://www.runoob.com/java/java-collections.html
 * [Java ArrayList] https://www.runoob.com/java/java-arraylist.html
 *
 * @author Jack魏
 */
public class Point01 {

    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

    public static void main(String[] args) {
        List<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
        arrayList.add("5");
        arrayList.add("6");

        arrayList.remove(1);

        arrayList.get(1);

        arrayList.contains("2");

        System.out.println(grow(999));
    }

    private static int grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = 1024;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);

        return newCapacity;
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    }
}
